package com.wsx.seven.a;

import java.util.Arrays;

public class Main0006 {

    public static void main(String[] args) {
        int[] array = new int[]{1, 6, 3, 9, 5, 10, 7, 8};
        shellSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void shellSort(int[] array) {
        int mid = array.length / 2;
        while (mid >= 1) {
            coreSort(array, mid);
            mid = mid / 2;
        }
    }

    private static void coreSort(int[] array, int dk) {
        for (int i = dk; i < array.length; i++) {
            if (array[i-dk] > array[i]) {
                int temp = array[i];
                array[i] = array[i - dk];
                int j;
                for (j = i - dk; j >= 0 && array[j] > temp; j = j - dk) {
                    array[j + dk] = array[j];
                }
                array[j+dk] = temp;
            }
        }

    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
